package com.breeze.networkproject;

import android.os.Bundle;
import android.util.Log;
import android.widget.ImageView;

import androidx.appcompat.app.AppCompatActivity;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.NetworkImageView;
import com.breeze.networkproject.volley.VolleyInterface;
import com.breeze.networkproject.volley.VolleyRequest;
import com.breeze.networkproject.volley.image.BitmapCache;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";
    private ImageView mImageView;
    private NetworkImageView mNetworkImageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mImageView = findViewById(R.id.image);
        mNetworkImageView = findViewById(R.id.network_image);
        // 5个方面的学习掌握
        // volleyGet();
        // volleyPost();
        // volleyWrapperGet();
        // volleyWrapperPost();
        requestImage();
        test();
    }

    @Override
    protected void onStop() {
        super.onStop();
        // 会取消对应TAG的网络请求
        MyApp.queues.cancelAll("volleyGet");
    }

    private void volleyGet() {
        String url = "http://api.k780.com/?app=phone.get&phone=13800138000&appkey=10003&sign=b59bc3ef6191eb9f747dd4e83c99f2a4&format=json";
        /*StringRequest stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Log.i(TAG, "onResponse: " + response);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                 Log.i(TAG, "onErrorResponse: " + error.getMessage());
            }
        });
        // 设置TAG，方便在全局队列中查找
        stringRequest.setTag("volleyGet");
        MyApp.queues.add(stringRequest);*/

        // 第三个参数为JSONObject，为传入的参数，对于post请求传参可以在这设置
        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                Log.i(TAG, "onResponse: " + response);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.i(TAG, "onErrorResponse: " + error.getMessage());
            }
        });
        jsonObjectRequest.setTag("volleyGet");
        MyApp.queues.add(jsonObjectRequest);

        // 对于 JsonArrayRequest 就不再演示
    }

    private void volleyPost() {
        String url = "http://api.k780.com/";
        /*StringRequest stringRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Log.i(TAG, "onResponse: " + response);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.i(TAG, "onErrorResponse: " + error.getMessage());
            }
        }) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                HashMap<String, String> params = new HashMap<>();
                params.put("app", "phone.get");
                params.put("phone", "13800138000");
                params.put("appkey", "10003");
                params.put("sign", "b59bc3ef6191eb9f747dd4e83c99f2a4");
                params.put("format", "json");
                return params;
            }
        };
        stringRequest.setTag("volleyPost");
        MyApp.queues.add(stringRequest);*/

        // 注意：测试通过该方式传入的参数到服务器为json字符串，服务器无法解析就会返回错误，关于解决办法，请参考
        // https://blog.csdn.net/stzy00/article/details/51603520?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-4.nonecase&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-4.nonecase
        HashMap<String, String> params = new HashMap<>();
        params.put("app", "phone.get");
        params.put("phone", "13800138000");
        params.put("appkey", "10003");
        params.put("sign", "b59bc3ef6191eb9f747dd4e83c99f2a4");
        params.put("format", "json");
        JSONObject jsonRequest = new JSONObject(params);
        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, url, jsonRequest, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                Log.i(TAG, "onResponse: " + response);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.i(TAG, "onErrorResponse: " + error.getMessage());
            }
        });
        jsonObjectRequest.setTag("volleyPost");
        MyApp.queues.add(jsonObjectRequest);
    }

    private void volleyWrapperGet() {
        String url = "http://api.k780.com/?app=phone.get&phone=13800138000&appkey=10003&sign=b59bc3ef6191eb9f747dd4e83c99f2a4&format=json";
        String tag = "volleyWrapperGet";
        VolleyRequest.requestGet(url, tag, new VolleyInterface() {

            @Override
            protected void onSuccess(String response) {
                Log.i(TAG, "onSuccess: " + response);
            }

            @Override
            protected void onError(VolleyError error) {
                Log.i(TAG, "onError: " + error.getMessage());
            }
        });
    }

    private void volleyWrapperPost() {
        String url = "http://api.k780.com/";
        String tag = "volleyWrapperPost";
        HashMap<String, String> params = new HashMap<>();
        params.put("app", "phone.get");
        params.put("phone", "13800138000");
        params.put("appkey", "10003");
        params.put("sign", "b59bc3ef6191eb9f747dd4e83c99f2a4");
        params.put("format", "json");
        VolleyRequest.requestPost(url, tag, new VolleyInterface() {
            @Override
            protected void onSuccess(String response) {
                Log.i(TAG, "onSuccess: " + response);
            }

            @Override
            protected void onError(VolleyError error) {
                Log.i(TAG, "onError: " + error.getMessage());
            }
        }, params);
    }

    private void requestImage() {
        String url = "https://www.baidu.com/img/bdlogo.png";
        // 方式1 ImageRequest下载图片
        /*ImageRequest imageRequest = new ImageRequest(url, new Response.Listener<Bitmap>() {
            @Override
            public void onResponse(Bitmap response) {
                mImageView.setImageBitmap(response);
            }
        }, 0, 0, null, Bitmap.Config.RGB_565, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(MainActivity.this, error.getMessage(), Toast.LENGTH_SHORT).show();
            }
        });
        MyApp.queues.add(imageRequest);*/

        // 方式2 ImageLoader 结合ImageCache和LurCache
        ImageLoader imageLoader = new ImageLoader(MyApp.queues, new BitmapCache());
        ImageLoader.ImageListener listener = ImageLoader.getImageListener(mImageView, R.mipmap.ic_launcher, R.mipmap.ic_launcher_round);
        imageLoader.get(url, listener);

        // 方式3 ImageLoader + NetworkImageView
        mNetworkImageView.setDefaultImageResId(R.mipmap.ic_launcher);
        mNetworkImageView.setErrorImageResId(R.mipmap.ic_launcher_round);
        mNetworkImageView.setImageUrl(url, imageLoader);
    }

    private void test() {
        String url = "http://api.ipvideotalk.com/pro/download/search";
        Map<String, String> mParamMap = new HashMap<String, String>();
        mParamMap.put("platform", "GVC3212");
        mParamMap.put("upload", "0");
        mParamMap.put("version", "0.0.0.9");
        VolleyRequest.requestPost(url, "hehe", new VolleyInterface() {
            @Override
            protected void onSuccess(String response) {
                Log.i(TAG, "onSuccess: " + response);
            }

            @Override
            protected void onError(VolleyError error) {

            }
        }, mParamMap);
    }
}